package com.test.jsoup;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Demo {
	
	private static final String BASE_URL = "http://tools.2345.com";
	private static String FilePath = "";
	private static String FileDetailPath = "";
	private static String groupFile = "group.txt";
	private static File groupF;
	private static String itemFile = "item.txt";
	private static File itemF;
	private ArrayList<Group> mGroups = new ArrayList<Group>();
	
	private void updateGroup() {
		try {
			mGroups.clear();
			Document doc = Jsoup.connect("http://tools.2345.com/pianfang/").get();
			Element fenleiList = doc.getElementsByAttributeValue("class", "sub-menu clearfix").get(0);
			System.out.println(fenleiList.html());
			System.out.println("=============================================================");
			Elements lis = fenleiList.select("a[href]");
			for( int i = 0 ; i < lis.size(); i++ ) {
				Element temp = lis.get(i);
				String href = temp.attr("href");
				if( href.endsWith("htm") ) {
					Group group = new Group();
					group.mId = "" + i;
					group.mTitle = temp.text();
					group.mUrl = href;
					writeTxtFile(group.toString(), groupF);
//					group.print();
					updateGrouopItem(group);
//					mGroups.add(group);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void updateGrouopItem( Group group ) {
		try {
			Document doc = Jsoup.connect(BASE_URL+group.mUrl).get();
			Element itemList = doc.getElementsByAttributeValue("class", "intro-cateTab clearfix").get(0);
//			System.out.println(itemList.html());
			System.out.println("=============================================================");
			Elements lis = itemList.select("a[href]");
			for( int i = 0 ; i < lis.size(); i++ ) {
				Element temp = lis.get(i);
				String href = temp.attr("href");
				if( href.endsWith("htm") ) {
					Item item = new Item();
					item.mTitle = temp.text();
					item.mUrl = href;
					item.mId = "" + i;
					item.mGid = group.mId;
					writeTxtFile(item.toString(), itemF);
					item.print();
					updatePianfang(item);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void updatePianfang( Item item ) {
		try {
			int pages = 0;
			int count = 0;
			File itemPianfang = new File(FileDetailPath, item.getFileTitle());
			if( itemPianfang.exists() ) {
				itemPianfang.delete();
			}
			itemPianfang.createNewFile();
			Document doc = Jsoup.connect(BASE_URL+item.mUrl).get();
			Element pageElement = doc.getElementsByAttributeValue("class", "page").get(0);
			Elements pageList = pageElement.select("a[href]");
			pages = Integer.valueOf(pageList.get(pageList.size()-2).text());
			System.out.println("pages = " + pages );
			for( int i = 1 ; i <= pages ; i++ ) {
				String tagetUrl = BASE_URL+item.mUrl;
				if( 1 < i ) {
					tagetUrl = tagetUrl.replace(".htm", "_"+i+".htm");
				}
				System.out.println("page "+ i + " = " + tagetUrl );
				
				Document pianfangPage = Jsoup.connect(tagetUrl).get();
				Element pianfangTab = doc.getElementsByAttributeValue("class", "intro-cateTab-cont").get(0);
				Elements pianfangList = pianfangTab.select("a[target]");
				for( Element pianfang : pianfangList ) {
					PianFang pf = new PianFang();
					pf.mGId = item.mGid;
					pf.mIId = item.mId;
					pf.mId = "" + count++ ;
					pf.mUrl = pianfang.attr("href");
					pf.mTitle = pianfang.text();
					System.out.println(pf.mUrl);
					Document pfDeatil = Jsoup.connect(BASE_URL+pf.mUrl).get();
					Element pfd = pfDeatil.getElementsByAttributeValue("class", "prescription-detail").get(0);
					pf.mDetail = pfd.text().trim();
					System.out.println(pf.toString());
//					File detailFile = new File(FileDetailPath, pf.getFileName());
					writeTxtFile(pf.toString(), itemPianfang);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void writeTxtFile(String content , File file) {  
		try {
			RandomAccessFile raf = new RandomAccessFile(file, "rws");
			raf.seek(raf.length());  
			raf.write(content.getBytes("utf-8"));  
			raf.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
	}  
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		FilePath = System.getProperty("user.dir")+"/file/";
		FileDetailPath = FilePath + "/detail/";
		System.out.println(FilePath);
		File f = new File(FilePath);
		f.mkdir();
		f = new File(FileDetailPath);
		f.mkdir();
		try {
			groupF = new File(FilePath, groupFile);
			if( groupF.exists() ) {
				groupF.delete();
			}
			groupF.createNewFile();
			itemF = new File(FilePath, itemFile);
			if( itemF.exists() ) {
				itemF.delete();
			}
			itemF.createNewFile();
		} catch (IOException e) {
			e.printStackTrace();
		}
		Demo demo = new Demo();
		demo.updateGroup();
	}

}
